var lang=null;
var colaboxMainworkerPool=google.gears.factory.create('beta.workerpool');
var updateWorkid;
if (Browser.Engine.trident){
	  lang=window.navigator.userLanguage.toLowerCase();
}else{
	 if (window.MessageEvent && !document.getBoxObjectFor){
	     	if (navigator.userAgent.toLowerCase().indexOf("zh-cn")>0){
	     		 lang="zh-cn";
	      }else if(navigator.userAgent.toLowerCase().indexOf("zh-tw")>0){
	      	 lang="zh-cn";
	      }else{
	      	 lang="en-us";
	      }
   }else {
   		lang="en-us";
   }	  		
	
}


initializeWindows = function(){
     MUI.updateWindow=function(){
	     new MUI.Modal({
					id: 'updateWindow',
					title: null,
					loadMethod: 'xhr',
			    contentURL: 'pages/'+lang+'/updatewindow.html',
					type: 'modal',
					footerHeight:8,
					headerHeight:8,
					closable:false,
					collapsible:false,
					width:200,
					height: 50,
					onresize:function(){
						//alert($('pageWrapper').getStyle('width'));
					},				
					onBeforeBuild:function(){						
						$('notetext').disabled =true;	
					},
					onClose:function(){
						$('notetext').disabled =false;
						
					}		
				});
			}	

    MUI.loginWindow=function(){
	     new MUI.Modal({
					id: 'loginWindow',
					title: null,
					loadMethod: 'xhr',
			    contentURL: 'pages/'+lang+'/loginwindow.html',
					type: 'modal',
					footerHeight:8,
					headerHeight:24,
					width: 240,
		     	height: 180,
					closable:true,
					collapsible:false,	
					resizable:true,
					onresize:function(){
						//alert($('pageWrapper').getStyle('width'));
					},				
					onBeforeBuild:function(){						
						$('notetext').disabled =true;	
					},
					onClose:function(){
						$('notetext').disabled =false;
					}		
				});
			}

    MUI.aboutWindow=function(){
	     new MUI.Modal({
					id: 'aboutWindow',
					title: null,
					loadMethod: 'xhr',
			    contentURL: 'pages/'+lang+'/aboutwindow.html',
					type: 'modal',
					footerHeight:8,
					headerHeight:24,
					width: 290,
		     	height: 240,
					closable:true,
					collapsible:false,	
					resizable:true,
					onresize:function(){
						//alert($('pageWrapper').getStyle('width'));
					},				
					onBeforeBuild:function(){						
						$('notetext').disabled =true;	
					},
					onClose:function(){
						$('notetext').disabled =false;
					}		
				});
			}

	

		MUI.optionWindow=function(){
	     new MUI.Modal({
					id: 'OptionWindow',
					title: null,
					loadMethod: 'xhr',
			    contentURL: 'pages/'+lang+'/optionwindow.html',
					type: 'modal',
					footerHeight:8,
					headerHeight:24,
					width: 280,
		     	height: 260,
					closable:true,
					collapsible:false,	
					onBeforeBuild:function(){						
						$('notetext').disabled =true;	
					},
					onContentLoaded:function(){
						//alert("on load");
						
													
					},
					onClose:function(){
						//alert("on close");
						$('notetext').disabled =false;
						if ($('checkboxAutoCache').checked){
							updateFlag=0;
							colaboxMainworkerPool.sendMessage([{msgId:7}], updateWorkid);
						}
					},
					require: {
						//css: ['css/notepad.css','css/moodatatable.css'],
					//	js:['scripts/optionwindow-init.js'],
						onload: function(){
			         pb = new dwProgressBar({	      
								container: $('put-bar-here'),
								startPercentage: 0,
								speed:1000,
								boxID: 'dwProgressBox',
								percentageID: 'dwProgressPerc'
											});
			
							pb2 = new dwProgressBar({
											container: $('put-bar-here2'),
											startPercentage: 0,
											speed:1000,
											boxID: 'dwProgressBox2',
											percentageID: 'dwProgressPerc2'
											
										});
			         getOptionParams(0);

						}	
					}		
					
				});
			}



//MUI.optionWindow();
//alert("a");


		var updateFlag=1;
		var ColaboxmainLoop=function(){
		  var updateCount=0;
			colaboxMainworkerPool.onmessage = function(a, b, message) {
				if (message.sender == updateWorkid) {
						  if (message.body[0].msgId==0){
						  		//notepad.options.id=message.body[0].ret;
						  		 $('desktopFooter').innerText=message.body[0].val;
						  }	else if (message.body[0].msgId==1){
						  		//notepad.options.id=message.body[0].ret;
						  		 $('desktopFooter').innerText=message.body[0].val;
						  }else if (message.body[0].msgId==2){		 
						  	  //alert(message.body[0].ret); 
						  	  if (message.body[0].ret=="onlinestatus") {		  	  	
								  	  if (message.body[0].val==1){
								         $('onlinestatus').src="images_app/user_login.png";
								         $('desktopFooter').innerText="check Google online status (online). ";//+message.body[0].step+"--"+message.body[0].val;
								      }else{
								         $('onlinestatus').src="images_app/user_login_offline.png";
								         $('desktopFooter').innerText="check Google online status (offline).";//+message.body[0].step+"--"+message.body[0].val;
								      }
								       
						      }else if (message.body[0].ret=="true"){
						      	$('desktopFooter').innerText=message.body[0].val;
						      }else {
						      	$('desktopFooter').innerText=message.body[0].val;
						      }
						  }	else if (message.body[0].msgId==3){									  	  
						  	  $('googleAccountSaveRet').innerText=message.body[0].val;
						  	  if (message.body[0].ret=="true"){						  	  
						  	  	colaboxMainworkerPool.sendMessage([{msgId:2}], updateWorkid);
						  	  }else{
						  	  	$('onlinestatus').src="images_app/user_login_offline.png";
						  	  }
						  }	else if (message.body[0].msgId==4){				  	  
						  	
						  	  //alert(message.body[0].AutoCache);
						  	  if (message.body[0].AutoCache=="true"){				  	  	   
						  	  	   $('checkboxAutoCache').checked=true;
						  	  }else{
						  	  	   $('checkboxAutoCache').checked=false;
						  	  }
						  }	else if (message.body[0].msgId==5){				  	  
						  	  $('Gusername').value=message.body[0].Gusername==null?"":message.body[0].Gusername;
						  	  $('Gpassword').value=message.body[0].Gpassword==null?"":message.body[0].Gpassword;
						  }	  else if (message.body[0].msgId==6){				  	  
						  	  $('desktopFooter').innerText=message.body[0].val;
						  }	  else if (message.body[0].msgId==7){				  	  
						  	  if (message.body[0].ret=="Capture"){
						  	  	  //$('desktopFooter').innerText=message.body[0].status;
						      	  if (message.body[0].status=="start"){
						      	  	$('notetext').disabled =true;
						      	  	  MUI.updateWindow();				      	  	       	  	 
						      	  }else  if (message.body[0].status=="ing"){
						      	  	// $('updateDiv').innerText=message.body[0].val;					      	  	
						      	  	 if ($defined(document.getElementById("updateDiv")))	{
								      	  	 	if (updateCount%4==0){
										      	  	  $('updateDiv').innerText="Colabox System Update ";
										      	  }else  if (updateCount%4==1){
										      	  	  $('updateDiv').innerText="Colabox System Update .";
										      	  }else  if (updateCount%4==2){
										      	  	  $('updateDiv').innerText= "Colabox System Update ..";
										      	  }else{
										      	  	  $('updateDiv').innerText= "Colabox System Update ...";
										      	  }
										      	  updateCount++;
										      	  $('desktopFooter').innerText=message.body[0].val;
						      	  	 }
						      	  	 
						      	  }else if (message.body[0].status=="stop"){			
						      	  	  if (message.body[0].val!="nowindow"){		      	  			     
						      	         MUI.closeWindow(MUI.currentModal);
						      	       }
						      	      $('notetext').disabled =false;
						      	      $('desktopFooter').innerText="All data capture success .";
						      	      updateFlag=0;
						      	  }	 						      	
						      }
						  }	
				  //alert('Received message from worker ' + message.sender + ': \n ret:{' + message.body[0].ret+'\n value='+message.body[0].val+"}");
				}
						
			};
			//saveWorkId=notepad.mainWork.workerPool.createWorker("google.gears.workerPool.onmessage ="+String(saveWork));
			updateWorkid=colaboxMainworkerPool.createWorkerFromUrl('scripts/colabox-worker.js');
			
		
			
			//msgid=0  update
			//colaboxMainworkerPool.sendMessage([{msgId:0}], updateWorkid);	
			//msgid=1  delete localserver  store and set isFirstStart=1
		  //colaboxMainworkerPool.sendMessage([{msgId:1}], updateWorkid);
		
		
			
			
		}
    
	ColaboxmainLoop();    
    
  function autoSyncNotepad(){  	   
  	   if (updateFlag==0){
  	      colaboxMainworkerPool.sendMessage([{msgId:2}], updateWorkid)	
  	  }
  }  
	 
	clearCache=function(flag){
		if (flag==0){
			  //clearCache localserver
		    colaboxMainworkerPool.sendMessage([{msgId:1}], updateWorkid);
	  }else if (flag==1){
	  	  //drop colaboxdb all table
	  	  colaboxMainworkerPool.sendMessage([{msgId:0}], updateWorkid);
	  }
	}   
		
	SaveGoogleAccount=function(){	
		    colaboxMainworkerPool.sendMessage([{msgId:3},{Username:$(Gusername).value,Password:$(Gpassword).value}], updateWorkid);   
	}  
	
	getOptionParams=function(flag){
		  if (flag==0){
		     colaboxMainworkerPool.sendMessage([{msgId:4}], updateWorkid);  
		  }else if (flag==1){
		  	 colaboxMainworkerPool.sendMessage([{msgId:5}], updateWorkid);  
		  }   
	}	
	    
	setAutoCacheFlag=function(flag){	     
		     colaboxMainworkerPool.sendMessage([{msgId:6},{AutoCache:flag}], updateWorkid);  
	}  

  


}




initializeColumns = function() {


	
	new MUI.Column({
		id: 'mainColumn',
		placement: 'main',
		resizeLimit: [100, 200]
	});
	
	// Add panels to second side column
	
	new MUI.Panel({
		id: 'help-panel',
		loadMethod: 'xhr',
		contentURL: 'pages/'+lang+'/notepad.html',
		column: 'mainColumn',
		tabsURL: 'pages/'+lang+'/panel-tabs.html',
		padding: { top: 0, right: 0, bottom: 0, left: 0},
		scrollbars:false,
		resizable:true,
		onresize:function(){
			//alert("0");
		},
		onContentLoaded:function(){
			//alert($('pageWrapper').getStyle('width'));
			$('notetext').setStyle('height',$('pageWrapper').getStyle('height').toInt()-60);	
			//window.onresize();
			
		},
		require: {
			//css: ['css/notepad.css','css/moodatatable.css'],
			js:['scripts/moodatatable.js','scripts/notepad-init.js'],
			onload: function(){

				}	
		}	
	});



	MUI.myChain.callChain();
	
}

// Initialize MochaUI when the DOM is ready
window.addEvent('load', function(){ //using load instead of domready for IE8



var initLang=function(){
	if (lang=="zh-cn"){	
	    $('colaboxTitle').innerText="Colabox 可乐罐";
  }	
}
 
 initLang();


	MUI.myChain = new Chain();
	MUI.myChain.chain(
		function(){MUI.Desktop.initialize();},
		//function(){MUI.Dock.initialize();},
		function(){initializeColumns();},		
		function(){initializeWindows();}		
	).callChain();	
	
	var onlinestatusTimer = google.gears.factory.create('beta.timer');
	colaboxMainworkerPool.sendMessage([{msgId:7}], updateWorkid);
		  //colaboxMainworkerPool.sendMessage([{msgId:2}], updateWorkid);
	onlinestatusTimer.setInterval(function(){colaboxMainworkerPool.sendMessage([{msgId:2}], updateWorkid);},20000);
	
		 window.onbeforeunload = function() {
        return "Goodbye . Good Luck .";
    }

});


window.addEvent('resize', function(){ //using load instead of domready for IE8
  //alert($('pageWrapper').getStyle('width'));
  if ($defined(document.getElementById("notetext")))	{
			      	//alert($('pageWrapper').getStyle('height'));
			      //	$('notetext').setStyle('height',$('pageWrapper').getStyle('height').toInt()-60);	
			      	
	}

});
